package com.nbicc.survey.controller;

import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import com.nbicc.survey.po.Questionnaire;
import com.nbicc.survey.po.QuestionnaireTopicEntire;
import com.nbicc.survey.service.IQuestionnaireService;
import com.nbicc.survey.vo.QuestionnaireQueryCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(value = "调查问卷 问卷" , description = "问卷管理")
@RestController
@Validated
@RequestMapping(value = "/survey/questionnaire")
public class QuestionnaireController {

    @Resource(name = "QuestionnaireServiceImpl")
    private IQuestionnaireService questionnaireService;

    @ApiOperation(value = "查询")
    @GetMapping("/id/{id}")
    @ResponseBody
    public ResponseObj selectById(@PathVariable String id){
        Questionnaire questionnaire = questionnaireService.selectById(id);
        return ResponseObj.success().setResult(questionnaire);
    }

    @ApiOperation(value = "查询整体")
    @GetMapping("/entire/id/{id}")
    @ResponseBody
    public ResponseObj selectByIdEntire(@PathVariable String id){
        QuestionnaireTopicEntire questionnaireTopicEntire = questionnaireService.selectByIdEntire(id);
        return ResponseObj.success().setResult(questionnaireTopicEntire);
    }

    @ApiOperation(value = "添加")
    @PostMapping
    @ResponseBody
    public ResponseObj insert(@RequestBody Questionnaire questionnaire){
        questionnaireService.insert(questionnaire);
        return ResponseObj.success().setResult(questionnaire);
    }

    @ApiOperation(value = "整体添加")
    @PostMapping("/entire")
    @ResponseBody
    public ResponseObj insertEntire(@RequestBody @Valid QuestionnaireTopicEntire questionnaireTopicEntire){
        questionnaireService.insertEntire(questionnaireTopicEntire);
        return ResponseObj.success().setResult(questionnaireTopicEntire);
    }

    @ApiIgnore
    @ApiOperation(value = "修改")
    @PutMapping
    @ResponseBody
    public ResponseObj update(@RequestBody Questionnaire questionnaire){
        questionnaireService.updateById(questionnaire);
        return ResponseObj.success().setResult(questionnaire);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/id/{id}")
    @ResponseBody
    public ResponseObj delete(@PathVariable String id){
        questionnaireService.deleteById(id);
        return ResponseObj.success();
    }

    //↑crud========================================================================

    @ApiOperation(value = "按条件(分页等)")
    @PostMapping("/find")
    @ResponseBody
    public ResponseObj find(@RequestBody RequestPageObj<Map<String,Object>> requestPageObj){
        ResponseObj responseObj = questionnaireService.find(requestPageObj);
        return responseObj;
    }

    @ApiOperation(value = "获取")
    @PostMapping("/list")
    @ResponseBody
    public ResponseObj list(@RequestBody QuestionnaireQueryCondition questionnaireQueryCondition) throws Exception {
        List<Questionnaire> questionnaireList = questionnaireService.selectList(questionnaireQueryCondition);
        return ResponseObj.success().setResult(questionnaireList);
    }

    @ApiOperation(value = "开启")
    @PutMapping("/start/id/{id}")
    @ResponseBody
    public ResponseObj start(@PathVariable String id){
        questionnaireService.start(id);
        return ResponseObj.success();
    }

    @ApiOperation(value = "关闭")
    @PutMapping("/finish/id/{id}")
    @ResponseBody
    public ResponseObj finish(@PathVariable String id){
        questionnaireService.finish(id);
        return ResponseObj.success();
    }

}
